//
//  MainCollectionViewCell.m
//  Stream
//
//  Created by shaw on 16/8/7.
//  Copyright © 2016年 showshaw. All rights reserved.
//

#import "MainCollectionViewCell.h"
#import "MainCollectionViewModel.h"
#import "UIImageView+WebCache.h"

@interface MainCollectionViewCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UIButton *wholeButton;
@property (weak, nonatomic) IBOutlet UIView *wholeView;

@property (weak, nonatomic) IBOutlet UIView *dividerView;

@end


@implementation MainCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [_wholeButton addTarget:self action:@selector(cellDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellDidLongPressed:)];
    
    [self.wholeView addGestureRecognizer:longPress];
    
    self.wholeView.layer.cornerRadius = 5;
    self.wholeView.layer.masksToBounds = YES;
    
    self.deleteButton.hidden = YES;
}

-(void)setModel:(MainCollectionViewModel *)model
{
    _model = model;
    
    if (model) {
        if(model.type == CollectionTypeAdd)
        {
            [_wholeButton setImage:[UIImage imageNamed:@"function_add"] forState:UIControlStateNormal];
            
            _titleLable.text = @"";
            _iconImageView.hidden = YES;
            _dividerView.hidden = YES;
        }
        else
        {
            NSInteger index =  model.ID % 4;
            NSString * bgURL = [NSString stringWithFormat:@"home_item_text_background_%ld",index];
            
            _iconImageView.hidden = NO;
            _dividerView.hidden = NO;
            
            _titleLable.text = model.title;
            
            [_wholeButton setImage:[UIImage imageNamed:bgURL] forState:UIControlStateNormal];
            
            if([model.title isEqualToString:@"智能摄像头"] || [model.title isEqualToString:@"电视直播"])
            {
                _iconImageView.image = [UIImage imageNamed:model.iconUrl];
            }
            else
            {
                [_iconImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
            }
        }
    }
}


- (IBAction)cellDidClickDeleteButton:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(collectionDidClickedDeleteButton:)]) {
        [self.delegate collectionDidClickedDeleteButton:self];
    }
}

-(void)cellDidClicked:(MainCollectionViewCell*)cell
{
    if ([self.delegate respondsToSelector:@selector(collectionDidClicked:)]) {
        [self.delegate collectionDidClicked:self];
    }
}

-(void)cellDidLongPressed:(UILongPressGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateBegan && [self.delegate respondsToSelector:@selector(collectionDidLongPressed:)]) {
            [self.delegate collectionDidLongPressed:self];
    }
}
@end
